package com.wipz.eoa.iphone.toDoTask;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.toft.widgets.workflow.factory.ServiceFactory;
import com.toft.widgets.workflow.factory.impl.ServiceFactoryImpl;
import com.toft.widgets.workflow.model.Page;
import com.toft.widgets.workflow.model.TaskItem;
import com.toft.widgets.workflow.provider.ApprocalAssistance;
import com.toft.widgets.workflow.utils.DateConvertUtil;

/**
 * 
 * @author Biqing Zeng
 * @date 2012-2-7 上午10:57:53
 */
public class ToDoTaskOperate  {

	/**
	 * 待办Operate-页面初始化数据查询方法
	 * 
	 */
	public List doQuery(String userCode){
		//获取页面查询条件集合类：
		TaskItem taskItem = new TaskItem();
		
		//获取当前用户编号
		List list = new ArrayList();
		try {
			ApprocalAssistance.beginTransaction();
			Page page = new Page(200);
			//调用接口方法查询当前用户的待办任务
			List result = this.getServiceFactory().getWorkflowQueryService().findAllTaskList(userCode, taskItem, page).getResult();
			if (!result.isEmpty() || result.size() != 0) {
				for (int i = 0; i < result.size(); i++) {
					TaskItem task = (TaskItem) result.get(i);
					Map map = new HashMap();
					map.put("NOTENAME", task.getActivityName());
					map.put("CODE", task.getBusinessSN());
					map.put("MONEY", task.getBusinessMoney());
					map.put("SUBMITUSER", task.getSubmitUserName());
					// 创建时间
					map.put("CREATETIME", DateConvertUtil.dateToString(task.getCreateTime()));
					// 标识单据状态open：活动中（包括审批中和退回状态单据）
					map.put("STATUS", task.getState());
					map.put("BUISNESSTYPENAME", task.getBusinessName());
					map.put("ID", task.getBusinessRecordId());
					map.put("TASKID", task.getId());
				//	map.put("BUSINESSURL", task.getUrl());
					StringBuffer  bf = new StringBuffer();
					bf.append(task.getUrl());
					bf.append("?");
					bf.append("COMMITERCODE=");
					bf.append(userCode);
					bf.append("&");
					bf.append("id=");
					bf.append(task.getBusinessRecordId());
					bf.append("&");
					bf.append("taskId=");
					bf.append(task.getId());
					map.put("BUSINESSURL",bf.toString());
					// 流程实例ID
					map.put("PROCESSINSTANCEID", task.getProcessInstanceId());
					map.put("ISREFUSE", Boolean.toString(task.isRefuseToSubmitActivity()));
					map.put("TESTOBJECT", task.getWorkflowObject());
					list.add(map);
				}
			}
			ApprocalAssistance.commitTransaction();
		} catch (Exception e) {
			ApprocalAssistance.rollbackTransaction();
		}
		
		return list;
	}


	/**
	 * 调用工厂集合类
	 * @return 服务工厂接口
	 */
	private ServiceFactory getServiceFactory() {
		return ServiceFactoryImpl.getServiceFactory();
	}
}
